package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.util.Base64
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import java.io.ByteArrayOutputStream

class Infoactivity : AppCompatActivity() {
    companion object {
        const val REQUEST_IMAGE_CAPTURE = 1
    }
    private lateinit var imageBitmap: Bitmap
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info)

        val etName: EditText = findViewById(R.id.et_name)
        val etPhone: EditText = findViewById(R.id.et_phone)
        val rgGender: RadioGroup = findViewById(R.id.radioGroup)
        // 保存按钮点击事件
        val btnSave: Button = findViewById(R.id.btn_save)
        btnSave.setOnClickListener {
            val selectedGenderId = rgGender.checkedRadioButtonId
            val selectedGender = findViewById<RadioButton>(selectedGenderId).text.toString()
            // 保存用户输入的信息
            val intent=Intent()
            intent.putExtra("name",etName.text.toString())
            intent.putExtra("phone",etPhone.text.toString())
            intent.putExtra("gender", selectedGender)
            // 将 Bitmap 转换为 Base64 字符串
            val byteArrayOutputStream = ByteArrayOutputStream()
            imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
            val byteArray = byteArrayOutputStream.toByteArray()
            val encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT)
            intent.putExtra("image", encodedImage)
            setResult(RESULT_OK,intent)
            // 执行保存操作逻辑
            // 返回主界面
            finish()
        }
        // 拍照按钮点击事件
        val button= findViewById<Button>(R.id.button_image)
        button.setOnClickListener {
            dispatchTakePictureIntent()
        }
        // 取消按钮点击事件
        val btnCancel: Button = findViewById(R.id.btn_cancel)
        btnCancel.setOnClickListener {
            // 返回主界面
            finish()
        }
    }
    // 拍照逻辑
    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePictureIntent.resolveActivity(packageManager) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }
    // 拍照后的回调
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            imageBitmap = data?.extras?.get("data") as Bitmap
            val imageView = findViewById<ImageView>(R.id.imageView)
            imageView.setImageBitmap(imageBitmap)
        }
    }
}